package com.moleq.dialog;

import java.util.Calendar;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.device.DeviceManager;
import com.moleq.enums.TransactionType;
import com.moleq.logic.TransactionManager;
import com.moleq.logic.UIManager;
import com.moleq.mgpos.widget.NumberPad;
import com.moleq.mgpos.widget.NumberPad;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.mgpos.widget.NumberPad.NumberPadListener;
import com.moleq.mgpos.activities.LoginActivity;
import com.moleq.mgpos.activities.R;
import com.moleq.models.Functions;
import com.moleq.models.GlobalInfo;
import com.moleq.models.InventorySetting;
import com.moleq.models.PosLineCvvWic;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class InputCvvInfoDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private Button btnConfirm;
	private Button btnClose;
	private Context mContext;
	private NumberPad numberPanel;
	private DateSettingSelectionDialog mdssDialog;
	private InputDateInfoDialog ididDialog;
	private int mYear, mMonth, mDay;
	public int iStartDate = 0;
	public int iEndDate = 0;
	private DeviceManager mDeviceManager;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.buttonCvvInfoClose:
				onCloseWithReturn(R.id.buttonCvvInfoClose,new Object());
				break;
			case R.id.buttonCvvInfoConfirm:
				if (validation()) {
					String price = et_cvv_amount.getText().toString().equals("")?"0":et_cvv_amount.getText().toString();
					
					String tmp =price;
					double paid = Double.valueOf(tmp);
					if (!price.contains(".")) {
						paid /= 100; 
					}
					if (paid < 0 || paid > 99999) {
						return;
					}
					
					PosLineCvvWic mPosLineCvvWic = new PosLineCvvWic();
					mPosLineCvvWic.setVw_acct_no(et_cvv_id.getText().toString());
					mPosLineCvvWic.setVw_amount(paid);
					mPosLineCvvWic.setVw_check_no(et_cvv_check_id.getText().toString());
					mPosLineCvvWic.setVw_end_date(String.valueOf(iEndDate));
					mPosLineCvvWic.setVw_start_date(String.valueOf(iStartDate));
					mPosLineCvvWic.setVw_type("V");//WIC W CVV V
					mPosLineCvvWic.setVw_id(1);
					onCloseWithReturn(R.id.buttonCvvInfoConfirm,mPosLineCvvWic);
				}
				break;
			case R.id.et_cvv_st_date:
				if (GlobalInfo.getInstance().calendar_type) {
					mdssDialog.show(R.id.et_cvv_st_date,et_cvv_st_date.getText().toString());
				}else{
					ididDialog.show(R.id.et_cvv_st_date,et_cvv_st_date.getText().toString());
				}
				break;
			case R.id.et_cvv_end_date:
				if (GlobalInfo.getInstance().calendar_type) {
					mdssDialog.show(R.id.et_cvv_end_date,et_cvv_end_date.getText().toString());
				}else{
					ididDialog.show(R.id.et_cvv_end_date,et_cvv_end_date.getText().toString());
				}
				break;	
			}
		}

	};
	
	private EditText et_cvv_id,et_cvv_check_id,et_cvv_amount,et_cvv_st_date,et_cvv_end_date;
	
	public void onCloseWithReturn(int what,Object obj){
		mDeviceManager.enableAllScanner();
		mOnResultListener.onReturnValue(what,obj);
		dismiss();
	}


	public InputCvvInfoDialog(Context context, Object base) {
		super(context,R.style.dialog,base);
		constructor(context,base);
	}
	
	public InputCvvInfoDialog(Context context, int theme,Object base) {
		super(context, theme,base);
		constructor(context,base);
	}
	
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_cvv_info);
		this.setCancelable(false);

		et_cvv_id = (EditText)findViewById(R.id.et_cvv_id);
		et_cvv_check_id = (EditText)findViewById(R.id.et_cvv_check_id);
		et_cvv_amount = (EditText)findViewById(R.id.et_cvv_amount);
		et_cvv_st_date = (EditText)findViewById(R.id.et_cvv_st_date);
		et_cvv_end_date = (EditText)findViewById(R.id.et_cvv_end_date);
		
		btnConfirm = (Button) findViewById(R.id.buttonCvvInfoConfirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttonCvvInfoClose);
		btnClose.setOnClickListener(mOnClickListener);
		
		
		et_cvv_id.setOnTouchListener(onTouchListener);
		et_cvv_check_id.setOnTouchListener(onTouchListener);
		et_cvv_amount.setOnTouchListener(onTouchListener);
		et_cvv_st_date.setOnClickListener(mOnClickListener);
		et_cvv_end_date.setOnClickListener(mOnClickListener);
		
		mdssDialog = new DateSettingSelectionDialog(this.getContext(),this);
		ididDialog = new InputDateInfoDialog(this.getContext(), this);
		
		// number panel
		numberPanel = (NumberPad) this.findViewById(R.id.numberPadLogin);
		numberPanel.setStyle(NumberPad.LOGIN_STYLE);
		numberPanel.setNumberPadListener(numberPadListener);
		numberPanel.setInputEditText(et_cvv_id);

	}
	
	private NumberPadListener numberPadListener = new NumberPadListener() {

		@Override
		public void onClick(NumberPadButton button) {
			switch (button) {
			case ENTER:
				String cvv_id,cvv_check_id,cvv_amount;
				
				cvv_id = et_cvv_id.getText().toString().trim();
				if (cvv_id.equals("")) {
					numberPanel.setInputEditText(et_cvv_id);
					UIManager.setFoucesByControl(et_cvv_id);
					return;
				}

				cvv_check_id = et_cvv_check_id.getText().toString().trim();
				if (cvv_check_id.equals("")) {
					numberPanel.setInputEditText(et_cvv_check_id);
					UIManager.setFoucesByControl(et_cvv_check_id);
					return;
				}
				
				cvv_amount = et_cvv_amount.getText().toString().trim();
				if (cvv_amount.equals("")) {
					numberPanel.setInputEditText(et_cvv_amount);
					UIManager.setFoucesByControl(et_cvv_amount);
					return;
				}
				

			default:
				//
				break;
			}

		}

	};
	
	private OnTouchListener onTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			EditText ed = new EditText(mContext);
			int inType = ed.getInputType();
			ed.setInputType(InputType.TYPE_NULL);
			ed.onTouchEvent(event);
			ed.setInputType(inType);
			ed.setSelection(ed.getText().length());

			switch (v.getId()) {
			case R.id.et_cvv_id:

				if (et_cvv_id.isFocused()) {
					et_cvv_id.clearFocus();
				}
				UIManager.setFoucesByControl(et_cvv_id);
				numberPanel.setInputEditText(et_cvv_id);
				// etLoginId.requestFocus();
				break;
			case R.id.et_cvv_check_id:
				if (et_cvv_check_id.isFocused()) {
					et_cvv_check_id.clearFocus();
				}
				UIManager.setFoucesByControl(et_cvv_check_id);
				numberPanel.setInputEditText(et_cvv_check_id);
				// etPwd.requestFocus();
				break;
			case R.id.et_cvv_amount:
				if (et_cvv_amount.isFocused()) {
					et_cvv_amount.clearFocus();
				}
				UIManager.setFoucesByControl(et_cvv_amount);
				numberPanel.setInputEditText(et_cvv_amount);
				// etPwd.requestFocus();
				break;
			
			default:
				//
				break;
			}
			return true;
		}
	};
	
	public Boolean checkDate(String time){
		String age = "";
	    boolean bo = true;
	    
		age = MoleQUtil.fixDateTime2(time, "yyyyMMdd", "yyyy-MM-dd");
		if(age.equals("")) bo = false;
		 
		return bo;
	}
	
	public boolean validation(){
		
		if (et_cvv_id.getText().toString().equals("")) {
			MessageBox.show(mContext, "CVV id can not be empty");
			return false;
		}
		
		if (et_cvv_check_id.getText().toString().equals("")) {
			MessageBox.show(mContext, "CVV check id can not be empty");
			return false;
		}
		
		if (!et_cvv_st_date.getText().toString().equals("") && et_cvv_end_date.getText().toString().equals("")) {
			MessageBox.show(mContext, "End Date can not be empty");
			return false;
		}
		
		if (et_cvv_st_date.getText().toString().equals("") && !et_cvv_end_date.getText().toString().equals("")) {
			MessageBox.show(mContext, "Start Date can not be empty");
			return false;
		}
		
		if(!checkDate(et_cvv_st_date.getText().toString()) && !et_cvv_st_date.getText().toString().equals("")){
			MessageBox.show(mContext, "The start date is not vaild.");
			return false;
		}
		
		if(!checkDate(et_cvv_end_date.getText().toString()) && !et_cvv_end_date.getText().toString().equals("")){
			MessageBox.show(mContext, "The end date is not vaild.");
			return false;
		}
		
		if (!et_cvv_end_date.getText().toString().equals("") && !et_cvv_st_date.getText().toString().equals("")) {
			String sStartDate = MoleQUtil.fixDateTime2(
					et_cvv_st_date.getText().toString(), "yyyyMMdd",
					"yyyy-MM-dd");
			String sEndDate = MoleQUtil.fixDateTime2(
					et_cvv_end_date.getText().toString(), "yyyyMMdd",
					"yyyy-MM-dd");
		    iStartDate = Integer.valueOf(sStartDate);
			iEndDate = Integer.valueOf(sEndDate);
			if(iStartDate > iEndDate) {
				MessageBox.show(mContext, "The end date cannot be earlier than start date.");
				return false;
			}
			
			if (iEndDate < Integer.valueOf(MoleQUtil.getNowwithFormat("yyyyMMdd"))) {
				MessageBox.show(mContext, "It is expired.");
				return false;
			}
		}
		return true;
		
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		
		mDeviceManager = DeviceManager.getInstance();
		mDeviceManager.disableAllScanner();
		
		if (et_cvv_id.isFocused()) {
			et_cvv_id.clearFocus();
		}

		UIManager.setFoucesByControl(et_cvv_id);
		numberPanel.setInputEditText(et_cvv_id);
		
		if (TransactionManager.getInstance().TransType == TransactionType.CVV) {
			PosLineCvvWic processPosLineCvvWic = ServiceManager.getInstance().mPosLineCvvWicService.Find();
			et_cvv_id.setText(processPosLineCvvWic.getVw_acct_no());
			et_cvv_check_id.setText(processPosLineCvvWic.getVw_check_no());
			et_cvv_amount.setText(MoleQUtil.fixNumber(processPosLineCvvWic.getVw_amount()));
			
			et_cvv_st_date.setText(MoleQUtil.fixDateTime2(
					processPosLineCvvWic.getVw_start_date(), "yyyy-MM-dd",
					"yyyyMMdd"));
			et_cvv_end_date.setText(MoleQUtil.fixDateTime2(
					processPosLineCvvWic.getVw_end_date(), "yyyy-MM-dd",
					"yyyyMMdd"));
		}else{
			et_cvv_id.setText("");
			et_cvv_check_id.setText("");
			et_cvv_amount.setText("");
			et_cvv_st_date.setText("");
			et_cvv_end_date.setText("");
		}
		
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		super.onCloseWithReturn(pramClass, viewId, returnvalue);
		
		switch (viewId) {
		case R.id.et_cvv_st_date:
			et_cvv_st_date.setText(returnvalue);
			break;
		case R.id.et_cvv_end_date:
			et_cvv_end_date.setText(returnvalue);
			break;
		default:
			break;
		}
	}


	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
	
}
